
clear 
clear matrix
clear mata

set maxvar 10000

cd "C:\Replication files_JR Thesis\Chapter 2"

use BES2019_W23_Panel_v23.0.dta, clear

numlabel, add

**# 1. coding covariates 


/// personal income

recode p_gross_personalW7 (1/2=1 "Up to 9999") (3/4=2 "10 to 19999") (5/6=3 "20 to 29999") (7/8=4 "30 to 39999") (9/10=5 "40 to 49999") (11/12=6 "50 to 69999") (12/14=7 "70K and over") (15/16=8 "DK"), gen (incomerec7) 

recode p_gross_personalW10 (1/2=1 "Up to 9999") (3/4=2 "10 to 19999") (5/6=3 "20 to 29999") (7/8=4 "30 to 39999") (9/10=5 "40 to 49999") (11/12=6 "50 to 69999") (12/14=7 "70K and over") (15/16=8 "DK"), gen (incomerec10)

recode p_gross_personalW11 (1/2=1 "Up to 9999") (3/4=2 "10 to 19999") (5/6=3 "20 to 29999") (7/8=4 "30 to 39999") (9/10=5 "40 to 49999") (11/12=6 "50 to 69999") (12/14=7 "70K and over") (15/16=8 "DK"), gen (incomerec11)  

recode p_gross_personalW14 (1/2=1 "Up to 9999") (3/4=2 "10 to 19999") (5/6=3 "20 to 29999") (7/8=4 "30 to 39999") (9/10=5 "40 to 49999") (11/12=6 "50 to 69999") (12/14=7 "70K and over") (15/16=8 "DK"), gen (incomerec14) 


//// Education

/// Education

recode p_edlevelW7 (4/5=1) (0/3=0), gen (higheduc7)

recode p_edlevelW10 (4/5=1) (0/3=0), gen (higheduc10)

recode p_edlevelW11 (4/5=1) (0/3=0), gen (higheduc11)

recode p_edlevelW14 (4/5=1) (0/3=0), gen (higheduc)
rename higheduc higheduc14

//// Social grade

recode p_socgradeW7 p_socgradeW10 p_socgradeW11 p_socgradeW14 (8=.) 
recode p_socgradeW7 (7=.)


/// Age

sum ageW7 ageW10 ageW11 ageW14


///// Ethnicity 

recode p_ethnicityW7 (1/2=1 "White") (3/6=2 "Mixed") (7/10=3 "Asian") (11/13=4 "Black") (14=5 "Chinese") (15=6 "Other") (16=7 "DK"), gen (ethnicity7rec) 
recode p_ethnicityW10 (1/2=1 "White") (3/6=2 "Mixed") (7/10=3 "Asian") (11/13=4 "Black") (14=5 "Chinese") (15=6 "Other") (16=7 "DK"), gen (ethnicity10rec) 
recode p_ethnicityW11 (1/2=1 "White") (3/6=2 "Mixed") (7/10=3 "Asian") (11/13=4 "Black") (14=5 "Chinese") (15=6 "Other") (16=7 "DK"), gen (ethnicity11rec) 
recode p_ethnicityW14 (1/2=1 "White") (3/6=2 "Mixed") (7/10=3 "Asian") (11/13=4 "Black") (14=5 "Chinese") (15=6 "Other") (16=7 "DK"), gen (ethnicity14rec) 

recode p_ethnicityW7 (1=1 "White") (else=0), gen (ethnic7)
recode p_ethnicityW10 (1=1 "White") (else=0), gen (ethnic10)
recode p_ethnicityW11 (1=1 "White") (else=0), gen (ethnic11)
recode p_ethnicityW14 (1=1 "White") (else=0), gen (ethnic14)

**# 2. coding authoritarianism 

/// Wave 7 

recode auth1W7-auth4W7 (9999=.)
recode auth1W7 (2=1) (1=0), gen (auth1_W7)
recode auth2W7 (1=1) (2=0), gen (auth2_W7)
recode auth3W7 (2=1) (1=0), gen (auth3_W7)
recode auth4W7 (2=1) (1=0), gen (auth4_W7)

gen authW7=auth1_W7 + auth2_W7 + auth3_W7 + auth4_W7
gen authW701=authW7/4

tab auth1_W7

/// Wave 10

recode auth1W10-auth4W10 (9999=.)
recode auth1W10 (2=1) (1=0), gen (auth1_W10)
recode auth2W10 (1=1) (2=0), gen (auth2_W10)
recode auth3W10 (2=1) (1=0), gen (auth3_W10)
recode auth4W10 (2=1) (1=0), gen (auth4_W10)

gen authW10=auth1_W10 + auth2_W10 + auth3_W10 + auth4_W10
gen authW1001=authW10/4

/// Wave 11

recode auth1W11-auth4W11 (9999=.)
recode auth1W11 (2=1) (1=0), gen (auth1_W11)
recode auth2W11 (1=1) (2=0), gen (auth2_W11)
recode auth3W11 (2=1) (1=0), gen (auth3_W11)
recode auth4W11 (2=1) (1=0), gen (auth4_W11)

gen authW11=auth1_W11 + auth2_W11 + auth3_W11 + auth4_W11
gen authW1101=authW11/4

/// Wave 14

recode auth1W14-auth4W14 (9999=.)
recode auth1W14 (2=1) (1=0), gen (auth1_W14)
recode auth2W14 (1=1) (2=0), gen (auth2_W14)
recode auth3W14 (2=1) (1=0), gen (auth3_W14)
recode auth4W14 (2=1) (1=0), gen (auth4_W14)

gen authW14=auth1_W14 + auth2_W14 + auth3_W14 + auth4_W14
gen authW1401=authW14/4

**# 3. Coding DVs 

//// DV1: Immigration scale

recode immigSelfW7 immigSelfW10 immigSelfW11 immigSelfW14 immigCulturalW7 immigCulturalW10 immigCulturalW11 immigCulturalW14 (9999=.)

pwcorr immigSelfW7 immigCulturalW7 
pwcorr immigSelfW10 immigCulturalW10 
pwcorr immigSelfW11 immigCulturalW11 
pwcorr immigSelfW14 immigCulturalW14 

alpha immigSelfW7 immigCulturalW7, item
alpha immigSelfW10 immigCulturalW10, item
alpha immigSelfW11 immigCulturalW11, item
alpha immigSelfW14 immigCulturalW14, item

factortest immigSelfW7 immigCulturalW7
factortest immigSelfW10 immigCulturalW10
factortest immigSelfW11 immigCulturalW11
factortest immigSelfW14 immigCulturalW14

factor immigSelfW7 immigCulturalW7

factor immigSelfW7 immigCulturalW7, factors(1)
rotate, oblimin

	local values "7 10 11 14"
	foreach value of local values {
    gen im_culturalW`value'=(immigCulturalW`value'-1)/6
}

gen immigSelfW701=immigSelfW7/10
gen immigSelfW1001=immigSelfW10/10
gen immigSelfW1101=immigSelfW11/10
gen immigSelfW1401=immigSelfW14/10

	local values "7 10 11 14"
	foreach value of local values {
    gen im_pref_2W`value'=(im_culturalW`value' + immigSelfW`value'01)/2
}

summ im_pref_2W7
summ im_pref_2W10
summ im_pref_2W11
summ im_pref_2W14

//// DV2: EU integration


tab EUIntegrationSelfW7
tab EUIntegrationSelfW10
tab EUIntegrationSelfW11
tab EUIntegrationSelfW14

recode EUIntegrationSelfW7 EUIntegrationSelfW10 EUIntegrationSelfW11 EUIntegrationSelfW14 (9999=.)

*Reverse code integration - bigger score = more European

gen EUintRW7=(0-EUIntegrationSelfW7)+10
gen EUintRW10=(0-EUIntegrationSelfW10)+10
gen EUintRW11=(0-EUIntegrationSelfW11)+10
gen EUintRW14=(0-EUIntegrationSelfW14)+10

	local values "7 10 11 14"
	foreach value of local values {
    gen eu_intW`value'=EUintRW`value'/10
}

////// DV4: PTV scores (3 WAVES ONLY)

//// Labour

recode ptvLabW7 ptvLabW10 ptvLabW11 (9999=.)

gen ptvLabW701=ptvLabW7/10
gen ptvLabW1001=ptvLabW10/10
gen ptvLabW1101=ptvLabW11/10


/// Conservative

recode ptvConW7 ptvConW10 ptvConW11 (9999=.)

gen ptvConW701=ptvConW7/10
gen ptvConW1001=ptvConW10/10
gen ptvConW1101=ptvConW11/10

//// UKIP

recode ptvUKIPW7 ptvUKIPW10 ptvUKIPW11 (9999=.)

gen ptvUKIPW701=ptvUKIPW7/10
gen ptvUKIPW1001=ptvUKIPW10/10
gen ptvUKIPW1101=ptvUKIPW11/10

// renaming vars 

rename  authW701  auth01W7 
rename  authW1001  auth01W10 
rename  authW1101 auth01W11 
rename  authW1401 auth01W14

rename  immigSelfW701  immigSelf01W7
rename  immigSelfW1001 immigSelf01W10
rename   immigSelfW1101  immigSelf01W11 
rename  immigSelfW1401 immigSelf01W14

rename  ptvLabW701  ptvLab01W7
rename  ptvLabW1001  ptvLab01W10 
rename  ptvLabW1101 ptvLab01W11

rename  ptvConW701   ptvCon01W7 
rename  ptvConW1001  ptvCon01W10 
rename  ptvConW1101 ptvCon01W11

rename  ptvUKIPW701  ptvUKIP01W7 
rename  ptvUKIPW1001  ptvUKIP01W10 
rename  ptvUKIPW1101 ptvUKIP01W11

**# Coding political engagement

tab polAttentionW7

recode polAttentionW7 polAttentionW10 polAttentionW11 polAttentionW14 (9999=.)

*For random effects/hybrid models: 
gen polattW7=polAttentionW7/10
gen polattW10=polAttentionW10/10
gen polattW11=polAttentionW11/10
gen polattW14=polAttentionW14/10


/// Restrict to the variables to use 
  
  save temp.dta, replace
  
  use temp.dta, clear
  
  keep id wave7 wave10 wave11 wave14 ageW7 ageW10 ageW11 ageW14 higheduc* incomerec* p_socgradeW7 p_socgradeW10 p_socgradeW11 p_socgradeW14  p_ethnicityW7 p_ethnicityW10 p_ethnicityW11 p_ethnicityW14 auth01* auth1_W* auth2_W* auth3_W* auth4_W* immigSelf01W* ptvCon01* ptvUKIP01* ptvLab01* gender im_culturalW* im_pref* eu_intW* polattW* polAttentionW7 polAttentionW10 polAttentionW11 polAttentionW14
  
      // Generating standardized variables 
  
foreach var of varlist auth*7 auth*10 auth*11 auth*14 eu_intW7 eu_intW10 eu_intW11 eu_intW14 immigSelf01W7 immigSelf01W10 immigSelf01W11 immigSelf01W14 im_pref_2W7 im_pref_2W10 im_pref_2W11 im_pref_2W14 {
  summ `var'
  gen std_`var' = ((`var'-r(mean))/r(sd))
  }


  // data is in wide format
  
  duplicates report id
    drop if id==.
	
	 save temp.dta, replace
  
  
  // Restructure long format
  
    reshape long wave ageW higheduc incomerec p_socgradeW p_ethnicityW leftRight01W std_auth01W auth01W std_auth1_W  auth1_W auth2_W std_auth2_W auth3_W std_auth3_W auth4_W std_auth4_W immigSelf01W std_immigSelf01W im_econW im_culturalW im_pref_2W std_im_pref_2W eu_intW std_eu_intW ptvCon01W ptvUKIP01W ptvLab01W polattW  polAttentionW, i(id) j(time)
  
 
  fre wave
  
    
    // Keep only people who are present at a given wave
	
	
  keep if wave==1
  
  
  
  
    // Set as panel data
  
  
  xtset id time
  
  
    // structure of the panel
	
	  
  fre auth01W
  
  drop if auth01W==.
  
	
  xtdes
  

  	// N is  13085 - number of people in at least one of the four waves with non missing info on the authoritarianism var 
	
	d,s

	
		*Recode ethnicity: 

  recode p_ethnicityW (1=1 "white british") (else=0 "other"), gen(ethnic)
  
  tab ethnic
  tab p_ethnicityW
  
  save long_data.dta, replace
  
  
**# 4. Preliminary analysis: attrition, RI-CLPM data prep, over-time variation in covariates


/// ATTRITION ANALYSIS
	
	sort id time
	
	// Generate observation variables:
	
	bysort id: gen obs = _n
	
	bysort id: egen totobs = count(obs)
	
	// Generate dummy variables: 
	
	ta gender, gen(gend)
	ta p_ethnicityW, gen(eth)
	ta incomerec, gen(inc)
	ta p_socgradeW, gen(soc)
	
	format gend1 ageW ///
	eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 eth11 eth12 eth13 eth14 eth15 eth16 ///
	higheduc inc1 inc2 inc3 inc4 inc5 inc6 inc7 inc8 soc1 soc2 soc3 soc4 soc5 soc6 ///
	leftRight01W auth01W im_pref_2W %9.2f
  
		//  full sample 
	tabstat gend1 ageW ///
	eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 eth11 eth12 eth13 eth14 eth15 eth16 ///
	higheduc inc1 inc2 inc3 inc4 inc5 inc6 inc7 inc8 soc1 soc2 soc3 soc4 soc5 soc6 ///
  auth01W im_pref_2W eu_intW , col(stat) f
	
  	//  observed more than once
  tabstat gend1 ageW ///
	eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 eth11 eth12 eth13 eth14 eth15 eth16 ///
	higheduc inc1 inc2 inc3 inc4 inc5 inc6 inc7 inc8 soc1 soc2 soc3 soc4 soc5 soc6 ///
	auth01W im_pref_2W eu_intW if totobs>1 , col(stat) f  
	
	save long_data.dta, replace
	
**# 5. RICLPM coding
       
  drop p_ethnicityW obs eth1 eth2 eth3 eth4 eth5 eth6 eth7 eth8 eth9 eth10 eth11 eth12 eth13 eth14 eth15 eth16 inc1 inc2 inc3 inc4 inc5 inc6 inc7 inc8 soc1 soc2 soc3 soc4 soc5 soc6 ptvCon01W ptvUKIP01W ptvLab01W
  
  reshape wide wave ageW higheduc incomerec p_socgradeW gender ethnic auth01W auth1_W auth2_W auth3_W auth4_W  immigSelf01W std_auth01W std_auth1_W std_auth2_W std_auth3_W std_auth4_W std_immigSelf01W  im_econW im_culturalW im_pref_2W std_im_pref_2W eu_intW std_eu_intW polAttentionW polattW, i(id) j(time)

  
  gen polatt_v=polAttentionW7 if auth01W7!=.
replace polatt_v=polAttentionW10 if auth01W7==. & auth01W10!=. 
replace polatt_v=polAttentionW11 if auth01W7==. & auth01W10==. & auth01W11!=.
replace polatt_v=polAttentionW14 if auth01W7==. & auth01W10==. & auth01W11==. & auth01W14!=.

tab polatt_v
summ polatt_v, detail
gen polatt_group=0 if polatt_v<7
replace polatt_group=1 if polatt_v>7&polatt_v<11
tab polatt_group

  *Labels:
	tab ethnic7
	tab p_socgradeW7
	tab incomerec7
	
	label drop _all
	
	save RICLPM_data.dta, replace 
  
  export delimited RICLPM_data.csv
  

**# 6. Random effects models 

use long_data.dta, clear

* immigration model 

xtreg im_pref_2W ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec auth01W b7.time b1.gender, re vce(cluster id)

*EU model 

xtreg eu_intW ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec auth01W b7.time b1.gender, re vce(cluster id)

* vote choice - UKIP 
xtreg ptvUKIP01W ageW b0.higheduc b0.ethnic  b1.p_socgradeW b1.incomerec auth01W b7.time b1.gender, re vce(cluster id)

* vote choice - Conservative 
  
xtreg ptvCon01W ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec auth01W b7.time b1.gender, re vce(cluster id)
	  
*vote choice - Labour 
  
xtreg ptvLab01W ageW b0.higheduc b0.ethnic  b1.p_socgradeW b1.incomerec auth01W b7.time b1.gender, re vce(cluster id)

**# 7. Fixed effects models 

*immigration model 

xtreg im_pref_2W ageW b1.incomerec auth01W b7.time b1.p_socgradeW, fe

*EU model 

xtreg eu_intW ageW b1.incomerec auth01W b7.time b1.p_socgradeW, fe 	
	
* vote choice - UKIP 

xtreg ptvUKIP01W ageW b1.incomerec auth01W b7.time b1.p_socgradeW, fe 

* vote choice - Conservative 

xtreg ptvCon01W ageW b1.incomerec auth01W b7.time b1.p_socgradeW, fe 

*vote choice - Labour 

xtreg ptvLab01W ageW b1.incomerec auth01W b7.time b1.p_socgradeW, fe 


**# 8. Interactions - engagement

// Random effects 

 xtreg im_pref_2W c.auth01W#c.polattW ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec auth01W polattW b7.time b1.gender, re vce(cluster id)
 margins, dydx(auth01W) at(polattW=(0.421 0.685 0.922))
 
 xtreg eu_intW c.auth01W#c.polattW ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec auth01W polattW b7.time b1.gender, re vce(cluster id)
 margins, dydx(auth01W) at(polattW=(0.421 0.685 0.922))
 
 xtreg ptvUKIP01W c.auth01W#c.polattW auth01W polattW ageW b0.higheduc b0.ethnic  b1.p_socgradeW b1.incomerec b7.time b1.gender, re vce(cluster id)
   margins, dydx(auth01W) at(polattW=(0.421 0.685 0.922))
   
 xtreg ptvCon01W c.auth01W#c.polattW auth01W polattW ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec auth01W b7.time b1.gender, re vce(cluster id)
   margins, dydx(auth01W) at(polattW=(0.421 0.685 0.922))
   
 xtreg ptvLab01W c.auth01W#c.polattW auth01W polattW ageW b0.higheduc b0.ethnic  b1.p_socgradeW b1.incomerec auth01W b7.time b1.gender, re vce(cluster id)
 margins, dydx(auth01W) at(polattW=(0.421 0.685 0.922))

 
// Hybrid models 

*All interactions are non-significant, 

xtreg im_pref_2W c.WPauth#c.polattW WPauth BPauth ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec polattW b7.time b1.gender, re vce(cluster id)
  margins, dydx(WPauth) at(polattW=(0.421 0.685 0.922))
	
xtreg eu_intW c.WPauth#c.polattW WPauth BPauth ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec polattW b7.time b1.gender, re vce(cluster id)
   margins, dydx(WPauth) at(polattW=(0.421 0.685 0.922))
	
xtreg ptvUKIP01W c.WPauth#c.polattW WPauth BPauth ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec polattW b7.time b1.gender, re vce(cluster id)
   margins, dydx(WPauth) at(polattW=(0.421 0.685 0.922))
	
xtreg ptvCon01W c.WPauth#c.polattW WPauth BPauth ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec polattW b7.time b1.gender, re vce(cluster id)
  margins, dydx(WPauth) at(polattW=(0.421 0.685 0.922))
	
xtreg ptvLab01W c.WPauth#c.polattW WPauth BPauth ageW b0.higheduc b0.ethnic b1.p_socgradeW b1.incomerec polattW b7.time b1.gender, re vce(cluster id)
  margins, dydx(WPauth) at(polattW=(0.421 0.685 0.922))
	
 
 